<?php

namespace App\Http\Resources;

use App\Helpers\BaseResource;
use App\Helpers\FileTool;

class Liveroom extends BaseResource
{

    public function toArray($request)
    {
        if ($this->rs_type == 'promote') {
            return $this->promote();
        }
        if ($this->rs_type == 'open-list') {
            return $this->filterFields([
                'id' => hashid_encode($this->id),
                'name' => $this->title,
                'description' => $this->description,
                'cover' => FileTool::thumb($this->avatar)
            ]);
        }
        return $this->filterFields([
            'id' => hashid_encode($this->id),
            'title' => $this->title,
            'description' => $this->description,
            'avatar' => FileTool::thumb($this->avatar),
            'qrcode' => FileTool::thumb($this->qrcode),
            'is_follow' => $this->when($this->ckFields('is_follow'), function () {
                return (boolean)\App\Models\Liveroom::judgeFollow($this, $this->getUser());
            }),
            'follow_number' => $this->when($this->ckFields('follow_number'), function () {
                return (string)\App\Models\Liveroom::getFollowNumber($this);
            }),
            'banner' => $this->when($this->ckFields('banner'), function () {
                return LiveroomBannerCollection::make($this->banner)->type('vux');
            })
        ]);
    }

    private function promote()
    {
        return $this->filterFields([
            'id' => hashid_encode($this->id),
            'name' => $this->title,
            'cover' => $this->avatar,
            'describe' => $this->describe ?? $this->description,
            'is_follow' => $this->when($this->ckFields('is_follow'), function () {
                return (boolean)\App\Models\Liveroom::judgeFollow($this, $this->getUser());
            }),
            'follow_number' => $this->when($this->ckFields('follow_number'), function () {
                return (string)\App\Models\Liveroom::getFollowNumber($this);
            }),
            'channel_number' => $this->when($this->ckFields('channel_number'), function () {
                return (int)\App\Models\Liveroom::getChannelNumber($this);
            }),
            'student_number' => $this->when($this->ckFields('student_number'), function () {
                return (int)\App\Models\Liveroom::getStudentNumber($this);
            }),
        ]);

    }
}
